package com.googlecode.olabs.gui.client;

import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class PrefsPage extends Composite {

	private static final String PREFS_URL = "/prefs/key";
	private static final String RADIO_GROUP_API_KEY = "apiKey";

	public PrefsPage() {
		final FlexTable mainTable = new FlexTable();
		initWidget(mainTable);

		final RadioButton radioApiKeyOff = new RadioButton(RADIO_GROUP_API_KEY,
				"Off");
		final RadioButton radioApiKeyOn = new RadioButton(RADIO_GROUP_API_KEY,
				"On");
		final TextBox apiKeyTextBox = new TextBox();
		apiKeyTextBox.setReadOnly(true);

		mainTable.setWidget(0, 0, new Label("Api key"));
		mainTable.getFlexCellFormatter().setRowSpan(0, 0, 2);
		mainTable.setWidget(0, 1, radioApiKeyOff);
		mainTable.setWidget(1, 0, radioApiKeyOn);
		mainTable.setWidget(0, 2, apiKeyTextBox);
		mainTable.getFlexCellFormatter().setRowSpan(0, 2, 2);
		mainTable.setWidget(0, 3, new Button("Save", new ClickListener() {
			public void onClick(Widget sender) {
				RequestBuilder builder = new RequestBuilder(
						RequestBuilder.POST, PREFS_URL);
				try {
					String apiKey = "1";
					if (radioApiKeyOff.isChecked()) {
						apiKey = "0";
					}
					builder.sendRequest(apiKey, new RequestCallback() {
						public void onError(Request request, Throwable exception) {
							Window.alert("Error saving Api Key setting"
									+ exception);
						}

						public void onResponseReceived(Request request,
								Response response) {
							if (response.getText().equals("0")) {
								apiKeyTextBox.setText("");
							} else {
								apiKeyTextBox.setText(response.getText());
							}
							Window.alert("Saved");
						}
					});
				} catch (RequestException e) {
					Window.alert("Exception making request to save apiKey: "
							+ e);
				}
			}
		}));
		mainTable.getFlexCellFormatter().setRowSpan(0, 3, 2);

		RequestBuilder builder = new RequestBuilder(RequestBuilder.GET,
				PREFS_URL);
		try {
			builder.sendRequest(null, new RequestCallback() {
				public void onError(Request request, Throwable exception) {
					Window.alert("Error getting Api Key setting" + exception);
				}

				public void onResponseReceived(Request request,
						Response response) {
					final String apiKey = response.getText();
					if (apiKey == null || apiKey.trim().isEmpty()
							|| apiKey.equals("0")) {
						radioApiKeyOff.setChecked(true);
						apiKeyTextBox.setText("");
					} else {
						radioApiKeyOn.setChecked(true);
						apiKeyTextBox.setText(apiKey);
					}
				}
			});
		} catch (RequestException e) {
			Window.alert("Exception making request for apiKey: " + e);
		}
	}
}
